package net.rock.eshop.utils;

import net.rock.eshop.entity.User;
import net.rock.eshop.exception.UserNotLogin;
import net.rock.eshop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class UserIdGetter {
    @Autowired
    private UserService userService;

    public int getUserIdBySessionId(String sessionId) {
        if (sessionId.isEmpty()) {
            throw new UserNotLogin();
        }

        User user = userService.fetchUserBySessionId(sessionId);

        if (user == null) {
            throw new UserNotLogin();
        }

        return user.getId();
    }
}
